//准备视图对象
	window.viewObj = {
		tbData: [{
			id: '-1',
			attrCode: '',
			attrName: '',
      idx: 100,
			resourceId: ''
		}]
	};




	//layui 模块化引用
	layui.use(['jquery', 'table', 'layer','ax','form','admin'], function(){
		var $ = layui.$, table = layui.table, form = layui.form, layer = layui.layer;
		var $ax = layui.ax;
    var admin = layui.admin;

		var ajax = new $ax(Sam.ctxPath + '/sam/resource/list/'+$("#menuId").val());
		var result = ajax.start();
		viewObj.tbData = result.data;


		//数据表格实例化
		var tbWidth = $("#tableRes").width();
		var layTableId = "layTable";
		var tableIns = table.render({
			elem: '#dataTable',
			id: layTableId,
			data: viewObj.tbData,
			width: tbWidth,
			page: false,
			loading: true,
			even: false, //不开启隔行背景
			cols: [[
				{title: '序号', type: 'numbers'},
				{field: 'attrCode', title: '字段属性名', edit: 'text'},
				{field: 'attrName', title: '字段显示名称', edit: 'text'},
                {field: 'idx', title: '排序', edit: 'number'},

				{field: 'id', title: '操作', templet: function(d){
					return '<a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del" lay-id="'+ d.id +'"><i class="layui-icon layui-icon-delete"></i>删除</a>';
				}}
			]],
			done: function(res, curr, count){
				viewObj.tbData = res.data;
			}
		});

		//定义事件集合
		var active = {
			addRow: function(){	//添加一行
				var oldData = table.cache[layTableId];
				//console.log(oldData);
				var newRow = {id: -1,idx:100, attrCode: null, attrName: ''};
				oldData.push(newRow);
				tableIns.reload({
					data : oldData
				});
			},
			updateRow: function(obj){
				var oldData = table.cache[layTableId];
				//console.log(oldData);
				for(var i=0, row; i < oldData.length; i++){
					row = oldData[i];
					if(row.id == obj.id){
						$.extend(oldData[i], obj);
						return;
					}
				}
				tableIns.reload({
					data : oldData
				});
			},
			removeEmptyTableCache: function(){
				var oldData = table.cache[layTableId];
				for(var i=0, row; i < oldData.length; i++){
					row = oldData[i];
					if(!row || !row.id){
						oldData.splice(i, 1);    //删除一项
					}
					continue;
				}
				tableIns.reload({
					data : oldData
				});
			},
			save: function(){
				var oldData = table.cache[layTableId];
				var datas = '';
				for(var i=0, row; i < oldData.length; i++){
					row = oldData[i];
					if(!row.attrCode){
						layer.msg("检查每一行，字段属性名不能为空！", { icon: 5 }); //提示
						return;
					}
					if(!row.attrName){
						layer.msg("检查每一行，字段显示名称不能为空！", { icon: 5 }); //提示
						return;
					}
					if(!row.idx){
						layer.msg("检查每一行，排序号不能为空！", { icon: 5 }); //提示
						return;
					}
					row.resourceId=$("#menuId").val();

					datas=datas+row.id+","+row.attrCode+","+row.attrName+","+row.idx+","+$("#menuId").val()+"|";

				}

				//datas="{"+datas+"0}";
				// document.getElementById("jsonResult").innerHTML = JSON.stringify(table.cache[layTableId], null, 2);	//使用JSON.stringify() 格式化输出JSON字符串
        //var data_=JSON.stringify(table.cache[layTableId]);
				var ajax = new $ax(Sam.ctxPath+"/sam/resource/save.json", function (data) {
						Sam.success("保存成功！");
					//	parent.layui.admin.events.refThisTabs();
						window.location.reload();

				}, function (data) {
						Sam.error("保存失败！" + data.responseJSON.message);
				});

 			//	console.log("---->"+JSON.parse(data_));
				ajax.set("data",datas);
				ajax.set("menuId",$("#menuId").val());

				ajax.start();
			  }
		}

		//激活事件
		var activeByType = function (type, arg) {
			if(arguments.length === 2){
				active[type] ? active[type].call(this, arg) : '';
			}else{
				active[type] ? active[type].call(this) : '';
			}
		}

		//注册按钮事件
		$('.layui-btn[data-type]').on('click', function () {
			var type = $(this).data('type');
			activeByType(type);
		});



		$('#btnCancel').click(function () {
			parent.layui.admin.events.closeThisTabs();
		});

		 //监听工具条
		table.on('tool(dataTable)', function (obj) {
			var data = obj.data, event = obj.event, tr = obj.tr; //获得当前行 tr 的DOM对象;
			//console.log(data);
			switch(event){
				case "del":
					layer.confirm('您真的要删除该字段吗？', function(index){
					  obj.del(); //删除对应行（tr）的DOM结构，并更新缓存
					  layer.close(index);
					  activeByType('removeEmptyTableCache');
					});
					break;
			}
		});
	});
